package com.tuling.netty.splitpackets;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;

import java.util.List;

public class MyMeaasgeDecode extends ByteToMessageDecoder {
    int length = 0;
    @Override
    protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List<Object> list) throws Exception {
        if(byteBuf.readableBytes() > 4){
            if(length == 0){
                length = byteBuf.readInt();
            }

            if(byteBuf.readableBytes() < length){
                System.out.println("数据不完整 还是有，请等待");
                return;
            }
            byte[] content = new byte[length];
            if(byteBuf.readableBytes() >= length){
                byteBuf.readBytes(content);
                MyMessageProtol myMessageProtol = new MyMessageProtol();
                myMessageProtol.setLen(length);
                myMessageProtol.setData(content);
                list.add(myMessageProtol);
            }
        }
    }
}
